package com.konsung.ksdc.manager.community;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 创建者     kgh
 * 创建时间   2016/6/14 18:29
 * 描述       增加联系人
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class AddContactActivity extends Activity {
    @Bind(R.id.edit_search)
    EditText editSearch;
    @Bind(R.id.btn_search)
    Button btnSearch;
    @Bind(R.id.lv_contact)
    ListView lvContact;
    @Bind(R.id.img_back)
    ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        ButterKnife.bind(this);

        initComponent();
    }

    /**
     * 初始化信息
     */
    private void initComponent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String condition = editSearch.getText().toString();
                if (StringUtils.isEmpty(condition)) {
                    Toast.makeText(MyApplication.mCurrentActivity,
                            getString(R.string.tip_input_condition),
                            Toast.LENGTH_SHORT)
                            .show();
                    return;
                }

                RequestUtils.findContactsByCondition(condition, new
                        RequestCallback() {

                            @Override
                            public void onError(Call call, Exception e) {
                                e.printStackTrace();
                                Toast.makeText(MyApplication.mCurrentActivity,
                                        getString(R.string.tip_loader_fail),
                                        Toast.LENGTH_SHORT)
                                        .show();
                            }

                            @Override
                            public void onResponse(ResponseBean responseBean) {
                                List<RegisterUserNetBean> users = responseBean.getList(
                                        new TypeToken<List<RegisterUserNetBean>>() {
                                        }.getType());
                                lvContact.setAdapter(null);
                                if (users.size() > 0) {
                                    showSearchResult(users);
                                } else {
                                    Toast.makeText(MyApplication.mCurrentActivity,
                                            getString(R.string.tip_query_none),
                                            Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }
                        });
            }
        });
    }

    /**
     * 显示查询结果
     *
     * @param users 用户信息
     */
    private void showSearchResult(final List<RegisterUserNetBean> users) {
        lvContact.setAdapter(new ContactListAdapter(users));
        lvContact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int
                    position, long l) {
                RegisterUserNetBean user = users.get(position);
                Intent intent = new Intent(AddContactActivity.this,
                        ContactIndexActivity.class);

                Gson gson = new Gson();
                intent.putExtra("data", gson.toJson(user));
                startActivity(intent);
            }
        });
    }
}
